<?php defined('BASEPATH') || exit('No direct script access allowed');

class Phone_model extends BF_Model
{
    protected $table_name	= 'building_phone';
	protected $key			= 'id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
	protected $set_created	= false;
	protected $set_modified = false;
	protected $soft_deletes	= true;

    protected $deleted_field     = 'deleted';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array();
	protected $after_insert 	= array();
	protected $before_update 	= array();
	protected $after_update 	= array();
	protected $before_find 	    = array();
	protected $after_find 		= array();
	protected $before_delete 	= array();
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(
		array(
			'field' => 'building_id',
			'label' => 'lang:phone_field_building_id',
			'rules' => 'required|trim|max_length[11]',
		),
		array(
			'field' => 'deleted',
			'label' => 'lang:phone_field_deleted',
			'rules' => 'max_length[1]',
		),
	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= false;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        $building_id = $post_data['building_id'];
        if (empty($building_id)) {
            return false;
        }
        $number = $post_data['number'];
        $platform = $post_data['platform'];
        $channel_id = $post_data['channel_id'];
        // 获取楼盘code
        $this->load->model('buildings/buildings_model');
        $building = $this->buildings_model->getBuildingById($building_id);
        $data['building_code'] = $building->code;
        if (is_array($number)) {
        	// 整理新增数据
        	$data = [];
        	foreach ($number as $key => $val) {
        		if (!empty($val)) {
        			$data[] = array('building_id'=>$building_id, 'platform'=>$platform[$key], 'channel_id'=>$channel_id[$key], 'number'=>$val, 'building_code'=>$building->code);
        		}
        	}
        }

        return $data;
    }

    /**
     * 给楼盘添加号码
     *
     * @param int $buildingId 楼盘ID
     * @param array $phones 号码数组
     * @return mixed 成功返回添加的条数，否则返回false
     */
    public function addPhones($buildingId, $data) {
        $return = false;
        if (is_array($data)) {
            $return = $this->db->insert_batch($this->table_name, $data);
        }
        return $return;
    }

    /**
     * 通过楼盘ID、渠道获取400号码，包含PC/MOBLIE
     * @param  int $buildingId 楼盘ID
     * @param  string $channel    渠道标示
     * @return mixed             array or false
     */
    public function getPhoneByBuildingId($buildingId, $channel) {
        $this->load->model('channel/channel_model');
    	$channel = $this->channel_model->getChannelByCode($channel);
    	if ($channel) {
    		$phone = $this->where(array('building_id'=>$buildingId, 'channel_id'=>$channel->id, 'deleted'=>0))->find_all();
    		if ($phone) {
    			foreach ($phone as $key => $val) {
    				$code = building_phone_platform_code()[$val->platform];
    				$data[$code] = $val;
    			}
    			return $data;
    		}
    	}
    	return false;
    }
}